package com.tanchengjin.admin.modules.cms.controller;

import com.github.pagehelper.PageHelper;
import com.tanchengjin.admin.modules.cms.mapper.ArticleCommentMapper;
import com.tanchengjin.admin.modules.cms.pojo.ArticleComment;
import com.tanchengjin.admin.service.ArticleCommentService;
import com.tanchengjin.admin.utils.LayuiAdminServerResponse;
import com.tanchengjin.admin.utils.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * article_comment Controller
 *
 * @author  TanChengjin
 * @since   v1.0.0
 * @version v1.0.0
 */
@Controller
@RequestMapping("${app.admin.prefix}/articleCommentManager")
public class ArticleCommentController {
    /**
    * Service
    */
    @Autowired
    private ArticleCommentService articleCommentService;

    /**
    * Mapper
    */
    @Autowired
    private ArticleCommentMapper articleCommentMapper;



    @GetMapping("/index")
    public String index() {
        return "/admin/article/comment/list";
    }

    @RequestMapping(value = "/list", method = {RequestMethod.GET})
    @ResponseBody
    public LayuiAdminServerResponse index(@RequestParam(value = "page", defaultValue = "1") int page, @RequestParam(value = "limit", defaultValue = "10") int limit, ArticleComment articleComment) {
        PageHelper.startPage(page, limit);
        List<ArticleComment> articleCommentList = articleCommentService.getAllByCondition(articleComment);

        long count = articleCommentService.count();
        return LayuiAdminServerResponse.responseWithSuccess(articleCommentList, String.valueOf(count));
    }

    @RequestMapping(value = "/create", method = {RequestMethod.GET})
    public String create(Model view) {
        return "/admin/article/comment/add";
    }

    /**
    * <p>
    *   POST http://www.example.com HTTP/1.1
    *   Content-Type: application/json;charset=UTF-8
    * </p>
    * @return ServerResponse
    */
    @RequestMapping(value = "", method = {RequestMethod.POST})
    @ResponseBody
    public ServerResponse store(@RequestBody ArticleComment articleComment) {
        int i = articleCommentService.create(articleComment);
        if (i >= 1) {
            return ServerResponse.responseWithSuccess();
        } else {
            return ServerResponse.responseWithFailureMessage("创建失败请重试");
        }
    }

    /**
    * <p>
    *   POST http://www.example.com HTTP/1.1
    *   Content-Type: application/x-www-form-urlencoded;charset=UTF-8
    * </p>
    * @return ServerResponse
    */
    @RequestMapping(value = "/submit", method = {RequestMethod.POST})
    @ResponseBody
    public ServerResponse submit(ArticleComment articleComment) {
        int i = articleCommentService.create(articleComment);
        if (i >= 1) {
            return ServerResponse.responseWithSuccess();
        } else {
            return ServerResponse.responseWithFailureMessage("创建失败请重试");
        }
    }

    @RequestMapping(value = "/{id}", method = {RequestMethod.GET})
    @ResponseBody
    public ServerResponse<ArticleComment> show(@PathVariable("id") int id) {
    ArticleComment articleComment = articleCommentService.findOneById(id);
        return ServerResponse.responseWithSuccess(articleComment);
    }

    @RequestMapping(value = "/{id}/edit", method = {RequestMethod.GET})
    public String edit(Model view, @PathVariable(value = "id") int id) {
    ArticleComment articleComment = articleCommentService.findOneById(id);
        view.addAttribute("articleComment", articleComment);
        return "/admin/article/comment/edit";
    }

    /**
    * <p>
    *   POST http://www.example.com HTTP/1.1
    *   Content-Type: application/json;charset=UTF-8
    * </p>
    * @return ServerResponse
    */
    @RequestMapping(value = "/{id}", method = {RequestMethod.PUT})
    @ResponseBody
    public ServerResponse update(@RequestBody ArticleComment articleComment, @PathVariable("id") int id) {
        int i = articleCommentService.updateById(articleComment, id);
        if (i >= 1) {
            return ServerResponse.responseWithSuccess();
        } else {
            return ServerResponse.responseWithFailureMessage("更新失败，请重试");
        }
    }

    @RequestMapping(value = "/{id}", method = {RequestMethod.DELETE})
    @ResponseBody
    public ServerResponse destroy(@PathVariable(value = "id") int id) {
        int i = articleCommentService.deleteById(id);

        if (i >= 1) {
            return ServerResponse.responseWithSuccess();
        } else {
            return ServerResponse.responseWithFailureMessage("删除失败请重试");
        }
    }

    @RequestMapping(value = "/batchDelete", method = {RequestMethod.DELETE})
    @ResponseBody
    public ServerResponse batchDelete(@RequestParam(value = "ids[]") int[] ids) {
        int i = articleCommentService.batchDelete(ids);
        if (i >= 1) {
            return ServerResponse.responseWithSuccess();
        } else {
            return ServerResponse.responseWithFailureMessage("删除失败请重试");
        }
    }

    /**
    * <p>
    *   POST http://www.example.com HTTP/1.1
    *   Content-Type: application/x-www-form-urlencoded;charset=UTF-8
    * </p>
    * @return ServerResponse
    */
    @RequestMapping(value = "/{id}/change", method = {RequestMethod.PUT})
    @ResponseBody
    public ServerResponse change(ArticleComment articleComment, @PathVariable("id") int id) {
        int i = articleCommentService.updateById(articleComment, id);
        if (i >= 1) {
            return ServerResponse.responseWithSuccess();
        } else {
            return ServerResponse.responseWithFailureMessage("更新失败，请重试");
        }
    }
}